package com.winHandle.load;

import com.common.FrameUtil;
import com.common.MessageShow;
import com.common.TimeChange;
import com.service.impl.ProjectServiceImpl;
import com.service.impl.TableServiceImpl;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainWin_P extends BaseFrame{

	private static final long serialVersionUID = 1409797556587284816L;

//	private JTextField search_T ;
//	private JButton searchBtn;
	private JButton addBtn;

	private JButton delBtn ;
	private JButton updateBtn ;

	private JTable jTable ;
	private DefaultTableModel model;

	private JLabel time = new JLabel(new SimpleDateFormat("yyyy-MM-dd    hh：mm：ss").format(new Date()));
	private TimeChange timeChange = new TimeChange(time);

	private JPanel top ;
	private JPanel center ;

	public void excute(){

		/***  布局内容   */
		top = new JPanel();

		addBtn = new JButton("  添 加  ");
		addBtn.addActionListener(this);

		top.add(new JLabel("  "));
		top.add(addBtn);

		//top.setLayout(new FlowLayout(FlowLayout.LEFT));
		jframe.getContentPane().add(top,BorderLayout.NORTH);

		center = new JPanel();

		jTable = new JTable();

		String[] titleArr ={"ID","名称"};

		model = new DefaultTableModel();
		model.setColumnIdentifiers(titleArr);
		for(int i = 0 ; i < 15 ; i++){
			model.insertRow(i, new Object[]{});
		}

		jTable.setModel(model);

		JScrollPane scrollpane = new JScrollPane(jTable);
		scrollpane.setPreferredSize(new Dimension(600,350));
		center.add(scrollpane);

		delBtn = FrameUtil.getJButton("  删 除  ", 150, 400, 120, 30);
		updateBtn = FrameUtil.getJButton("  修 改  ", 450, 400, 120, 30);

		jframe.add(delBtn);
		jframe.add(updateBtn);
		delBtn.addActionListener(this);
		updateBtn.addActionListener(this);

		jframe.getContentPane().add(center,BorderLayout.CENTER);

		new ProjectServiceImpl().loadModelData( model );

		jframe.setVisible(true);

	}

	/**
	 * 添加 、删除、 修改
	 * @param e
     */
	@Override
	public void actionPerformed(ActionEvent e) {

		super.actionPerformed(e);
		if (e.getSource() == addBtn) {

			new ProjectWin("添加表单",model);

		}else if (e.getSource() == delBtn) {

			int rowCount = jTable.getSelectedRowCount() ;
			if(rowCount < 1){
				MessageShow.warn("请选择要删除的记录！");
			}else if (rowCount > 1) {
				MessageShow.warn("每次只能删除一条记录！");
			}else{
				Object selectedCell = model.getValueAt(jTable.getSelectedRow(), 0);
				if(null == selectedCell){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				String id = selectedCell.toString();

				if(0 != MessageShow.confirm("确认要删除ID为 [ " + id + " ] 的记录吗？")){
					return ;
				}
				try {
					if(new ProjectServiceImpl().del(Long.parseLong(id))){
						MessageShow.error("删除成功！");
						new ProjectServiceImpl().loadModelData(model );
					}else {
						MessageShow.error("删除失败！");
					}
				} catch (NumberFormatException e1) {

					e1.printStackTrace();
					MessageShow.error("参数异常！");
				}

			}
		}else if (e.getSource() == updateBtn) {
			int rowCount = jTable.getSelectedRowCount() ;
			if(rowCount < 1){
				MessageShow.warn("请选择要修改的记录！");
			}else if (rowCount > 1) {
				MessageShow.warn("每次只能操作一条记录！");
			}else{
				Object selectedCell = model.getValueAt(jTable.getSelectedRow(), 0);
				if(null == selectedCell){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				String id = selectedCell.toString();
				if("".equals(id.trim())){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				new ProjectWin("项目修改", model, Long.parseLong(id), true);
			}
		}
	}

	public MainWin_P() {
		super();
		showFrameName = "PROJECT";
		excute();
	}
	public MainWin_P(String title){
		super(title + " -> 项目");
		showFrameName = "PROJECT";
		excute();
	}

}
